﻿using C1.Win.C1FlexGrid;
using C1.Win.C1Ribbon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TableJsonHelper;
using YNCNumberSys;

namespace JsonYNCNumsys
{
    public partial class FormNumber : C1RibbonForm
    {
        string m_fileName = string.Empty;
        public FormNumber()
        {
            InitializeComponent();
        }

        private void ribbonButtonImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件夹";
            dialog.Filter = "所有文件(*.*)|*.*";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string file = dialog.FileName;
                m_fileName = dialog.FileName;
                string s = string.Empty;
                s=File.ReadAllText(file);
                string sJson=EnOrDeEncryptHelper.DecryptText(s);
                DataTable dt=DataTableAndJsonHelper.JsonToDataTableWithJsonNet(sJson);
                if (!dt.Columns.Contains("路径"))
                {
                    dt.Columns.Add(new DataColumn("路径", typeof(string)));
                }
                if (!dt.Columns.Contains("flag"))
                {
                    dt.Columns.Add(new DataColumn("flag", typeof(int)));
                }

                if (dt!=null)
                    m_flex.DataSource = dt;
                //if(m_flex.Cols.Contains("图纸编号"))
                //    m_flex.Cols["图纸编号"].Visible=false;
                m_flex.AutoSizeCols();
                m_flex.AutoSizeRows();
                if (m_flex.Cols.Contains("零件序号"))
                {
                    m_flex.Cols["零件序号"].Style = m_flex.Styles["Button"]; 
                }

                if (m_flex.Cols.Contains("路径"))
                {
                    m_flex.Cols["路径"].Visible = false;
                }
                if (m_flex.Cols.Contains("flag"))
                {
                    m_flex.Cols["flag"].Visible = false;
                }

                m_flex.Cols["零件序号"].AllowEditing = false;
                m_flex.Update();
            }

        }
        private void ribbonButtonPaperBind_Click(object sender, EventArgs e)
        {
            int cur = m_flex.RowSel;
            if(cur<1)
            {
                MessageBox.Show("提示", "请选择图号所在的行", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                PaperBindForm dlg = new PaperBindForm();
                dlg.PaperNo= m_flex.Rows[cur]["图纸编号"].ToString();
                if(dlg.ShowDialog()==DialogResult.OK)
                {
                    m_flex.Rows[cur]["零件序号"] = dlg.DwgNo;
                }
            }
        }

        private void ribbonButtonSaveAs_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveImageDialog = new SaveFileDialog();
            saveImageDialog.Filter = "Json Files (*.json)|*.json";
            saveImageDialog.Title = @"导出图号分配结果";
            saveImageDialog.InitialDirectory = Path.GetDirectoryName(m_fileName);// !string.IsNullOrEmpty(m_fileName) ? m_fileName : ""; 
            saveImageDialog.FileName = Path.GetFileName(m_fileName);
            if (saveImageDialog.ShowDialog() == DialogResult.OK)
            {
                string savePath = saveImageDialog.FileName;
                string s = DataTableAndJsonHelper.DataTableToJsonWithJsonNet((DataTable)m_flex.DataSource);
                string sEnc = EnOrDeEncryptHelper.EncryptText(s);
                File.WriteAllText(savePath, sEnc);
            }
        }

        private void FormNumber_Load(object sender, EventArgs e)
        {
            if (!m_flex.Styles.Contains("Button"))
            {
                CellStyle csButton = m_flex.Styles.Add("Button");
                csButton.DataType = typeof(string);
                csButton.ComboList = @"...";
            }

        }

        private void m_flex_DoubleClick(object sender, EventArgs e)
        {
            int rowSel = m_flex.RowSel;
            int colSel = m_flex.ColSel;
            int cur = m_flex.RowSel;
            if (rowSel <0 && colSel <0)
            {
                MessageBox.Show("请选择正确的【零件序号】行、列。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if(m_flex.Cols[colSel].Caption != "零件序号")
                {
                    //MessageBox.Show("零件序号所在单元格。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                
                OpenFileDialog openDialog = new OpenFileDialog();
                openDialog.Filter = "图纸文件 (*.dwg)|*.dwg|所有文件(*.*)|*.*";
                openDialog.Title = @"选择图纸文件";
                if (openDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = Path.GetFileNameWithoutExtension(openDialog.FileName);
                    for(int i=0;i< m_flex.Rows.Count;i++)
                    {
                        if(m_flex[i, colSel]!=DBNull.Value)
                        {
                            if (m_flex[i, colSel].ToString() == filePath)
                            {
                                MessageBox.Show("图纸已经存在，不能重复分配！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                        }
                    }
                    m_flex[rowSel, colSel] = filePath;
                    m_flex.Rows[rowSel]["路径"] = openDialog.FileName;
                    m_flex.Rows[rowSel]["flag"] = 0;
                }
            }
        }

        private void ribbonButtonTitleWr_Click(object sender, EventArgs e)
        {
            DataTable dt=(DataTable)m_flex.DataSource;
            for (int rowSel = 1; rowSel < m_flex.Rows.Count; rowSel++)
            {
                if (m_flex.Rows[rowSel]["路径"]!= System.DBNull.Value)
                {
                    string sPath = (string)m_flex.Rows[rowSel]["路径"];
                    string sPartNum = m_flex.Rows[rowSel]["图纸编号"].ToString();
                    sPartNum=sPartNum.PadLeft(6,'0');
                    if (File.Exists(sPath))
                    {
                        string timeFoder = Path.Combine(Application.StartupPath, "图纸" + DateTime.Now.ToString("yyyyMMddHHmmss"));
                        if (!Directory.Exists(timeFoder))
                            Directory.CreateDirectory(timeFoder);
                        string fileName = (string)m_flex.Rows[rowSel]["图纸类型"]+ sPartNum+".dwg";
                        string fileNew = Path.Combine(timeFoder, fileName);
                        if(!File.Exists(fileNew))
                        {
                            File.Copy(sPath, fileNew);
                            if(File.Exists(fileNew))
                            {
                                foreach(DataRow dr in dt.Rows)
                                {
                                    if (dr["图纸编号"] != null)
                                    {
                                        if (dr["图纸编号"].ToString() == m_flex.Rows[rowSel]["图纸编号"].ToString())
                                        {
                                            CadHelperDal.SetTitle(fileNew, dr);
                                            break;
                                        }
                                    }
                                }
                                
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show(sPath + "不存在,单击【确定】继续。", "提示");
                    }
                }
                
            }
        }
    }
}
